package com.lisn.mylib.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;


import com.lisn.commonlib.LiveDataBus;
import com.lisn.mylib.R;


public class LiveDataBusTestActivity extends AppCompatActivity {

    private static final String TAG = LiveDataBusTestActivity.class.getSimpleName();

    public static final String EVENT_POST_VALUE = "EVENT_POST_VALUE";
    public static final String EVENT_POST_STICKY_DATA = "EVENT_POST_STICKY_DATA";

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, LiveDataBusTestActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_data_bus_test);

        LiveDataBus.get().with(LiveDataBusTestActivity.EVENT_POST_STICKY_DATA)
                .observe(this, new Observer() {
                    @Override
                    public void onChanged(Object o) {
                        Log.e(TAG, "onChanged: " + o.toString());
                    }
                });

        LiveDataBus.get().with(LiveDataBusTestActivity.EVENT_POST_VALUE)
                .observe(this, new Observer() {
                    @Override
                    public void onChanged(Object o) {
                        Log.e(TAG, "onChanged: " + o.toString());
                    }
                });
    }

    public void postValue(View view) {
        LiveDataBus.get().with(EVENT_POST_VALUE).postValue("1234abcd");
    }

    public void postStickyData(View view) {
        LiveDataBus.get().with(EVENT_POST_STICKY_DATA).postValue("1234abcd 1234abcd 1234abcd");
    }
}